package com.corpgovernment.job.dao.mapper;

import com.corpgovernment.job.dao.entity.db.EmployeeJobRelationDo;
import com.corpgovernment.job.enums.DeletedEnum;
import com.corpgovernment.organization.common.mybatis.TkMapper;
import com.ctrip.corp.obt.generic.utils.CollectionUtils;
import com.ctrip.corp.obt.generic.utils.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ldma
 * @date 2024/1/4
 */
public interface EmployeeJobRelationMapper extends TkMapper<EmployeeJobRelationDo> {

    default void removeByJobId(Long jobId) {
        if (jobId == null) {
            return;
        }
        EmployeeJobRelationDo employeeJobRelationDo = new EmployeeJobRelationDo();
        employeeJobRelationDo.setDeleted(DeletedEnum.TRUE.getCode());
        Example example = new Example(EmployeeJobRelationDo.class);
        example.createCriteria().andEqualTo("jobId", jobId).andEqualTo("deleted", DeletedEnum.FALSE.getCode());
        updateByExampleSelective(employeeJobRelationDo, example);
    }

    default List<EmployeeJobRelationDo> listAll() {
        Example example = new Example(EmployeeJobRelationDo.class);
        example.createCriteria().andEqualTo("deleted", DeletedEnum.FALSE.getCode());
        return selectByExample(example);
    }

    default EmployeeJobRelationDo getByUid(String uid) {
        if (StringUtils.isBlank(uid)) {
            return null;
        }
        Example example = new Example(EmployeeJobRelationDo.class);
        example.createCriteria().andEqualTo("deleted", DeletedEnum.FALSE.getCode()).andEqualTo("uid", uid);
        List<EmployeeJobRelationDo> employeeJobRelationDoList = selectByExample(example);
        return CollectionUtils.isEmpty(employeeJobRelationDoList) ? null : employeeJobRelationDoList.get(0);
    }

    default void createOrUpdate(EmployeeJobRelationDo employeeJobRelationDo) {
        String uid = employeeJobRelationDo.getUid();
        Long jobId = employeeJobRelationDo.getJobId();
        EmployeeJobRelationDo relation = getByUid(uid);
        if (relation == null) {
            insertSelective(EmployeeJobRelationDo.builder().jobId(jobId).uid(uid).build());
        } else {
            updateByPrimaryKeySelective(EmployeeJobRelationDo.builder().id(relation.getId()).uid(uid).jobId(jobId).build());
        }
    }

    default void removeByUid(String uid) {
        if (StringUtils.isBlank(uid)) {
            return;
        }
        EmployeeJobRelationDo employeeJobRelationDo = new EmployeeJobRelationDo();
        employeeJobRelationDo.setDeleted(DeletedEnum.TRUE.getCode());
        Example example = new Example(EmployeeJobRelationDo.class);
        example.createCriteria().andEqualTo("uid", uid).andEqualTo("deleted", DeletedEnum.FALSE.getCode());
        updateByExampleSelective(employeeJobRelationDo, example);
    }

    default List<EmployeeJobRelationDo> listByJobIdList(List<Long> jobIdList) {
        if (CollectionUtils.isEmpty(jobIdList)) {
            return new ArrayList<>(0);
        }
        Example example = new Example(EmployeeJobRelationDo.class);
        example.createCriteria().andIn("jobId", jobIdList).andEqualTo("deleted", DeletedEnum.FALSE.getCode());
        return selectByExample(example);
    }

    default List<EmployeeJobRelationDo> listByUidList(List<String> uidList) {
        if (CollectionUtils.isEmpty(uidList)) {
            return new ArrayList<>(0);
        }
        Example example = new Example(EmployeeJobRelationDo.class);
        example.createCriteria().andIn("uid", uidList).andEqualTo("deleted", DeletedEnum.FALSE.getCode());
        return selectByExample(example);
    }

    default void removeByUidListAndJobId(List<String> uidList, Long jobId) {
        if (CollectionUtils.isEmpty(uidList) || jobId == null) {
            return;
        }
        EmployeeJobRelationDo employeeJobRelationDo = new EmployeeJobRelationDo();
        employeeJobRelationDo.setDeleted(DeletedEnum.TRUE.getCode());
        Example example = new Example(EmployeeJobRelationDo.class);
        example.createCriteria().andIn("uid", uidList).andEqualTo("jobId", jobId).andEqualTo("deleted", DeletedEnum.FALSE.getCode());
        updateByExampleSelective(employeeJobRelationDo, example);
    }

}
